package com.yxh.www.crawler.parser;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xuxueli.crawler.parser.PageParser;
import com.xuxueli.crawler.proxy.ProxyMaker;
import com.xuxueli.crawler.proxy.strategy.RandomProxyMaker;
import com.xuxueli.crawler.proxy.strategy.RoundProxyMaker;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  芝麻HTTP IP获取接口 解析器
 * </p>
 *
 * @author yangxiaohui
 * @since 2020/5/15
 */
@Slf4j
public class ZmProxyIpParser extends PageParser {
    // 设置代理池
    private ProxyMaker randomProxyMaker = new RandomProxyMaker();
    private ProxyMaker roundProxyMaker = new RoundProxyMaker();

    @Override
    public void parse(Document document, Element element, Object o) {
        String proxyIpsStr=document.text();
        if (StringUtils.isNotBlank(proxyIpsStr)){
            String[] proxyIps=proxyIpsStr.split(",");
            List<Proxy> proxyList=new ArrayList<>();
            for (String proxyIpStr:proxyIps){
                String[] proxy = proxyIpStr.split(":");
                proxyList.add(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxy[0],Integer.parseInt(proxy[1]))));
            }
            randomProxyMaker.addProxyList(proxyList);
            roundProxyMaker.addProxyList(proxyList);
        }
        log.info("获取代理:{}", JSONObject.toJSONString(roundProxyMaker));
    }

    public ProxyMaker getRandomProxyMaker() {
        return randomProxyMaker;
    }

    public ProxyMaker getRoundProxyMaker() {
        return roundProxyMaker;
    }
}
